﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Plans;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Plans
{
    public class TeamTypeServices : ITeamTypeServices
    {
        public TeamTypeServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }
        //InsertAsync新增
        public async Task<bool> AddAsync(TeamType model)
        {
            //添加数据
            var result = await Db.InsertAsync(model);

            return result;
        }
        //删除数据
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete TeamType where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete TeamType where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
        //表格获取数据
        public async Task<IEnumerable<TeamType>> GetAllAsync()
        {
            string sql = "select * from TeamType";
            var list = await Db.QueryAllAsync<TeamType>(sql);
            return list;
        }
        //分页
        public async Task<IEnumerable<TeamType>> GetAsync(int PageIndex, int PageSize, string TypeCode, string TypeName, string Creator)
        {
            string sql = "select * from TeamType where 1=1 ";
            if (!string.IsNullOrEmpty(TypeCode))
            {
                sql += " and TypeCode like @TypeCode ";
            }
            if (!string.IsNullOrEmpty(TypeName))
            {
                sql += " and TypeName like @TypeName ";
            }
            if (!string.IsNullOrEmpty(Creator))
            {
                sql += " and Creator like @Creator ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<TeamType>(sql, new { PageIndex, PageSize, TypeCode = $"%{TypeCode}%", TypeName = $"%{TypeName}%", Creator = $"%{Creator}%" });
            return list;
        }
        //分页
        public async Task<int> GetCountAsync(string TypeCode, string TypeName, string Creator)
        {
            string sql = "select count(*) from TeamType where 1=1 ";
            if (!string.IsNullOrEmpty(TypeCode))
            {
                sql += "and TypeCode like @TypeCode ";
            }
            if (!string.IsNullOrEmpty(TypeName))
            {
                sql += "and TypeName like @TypeName ";
            }
            if (!string.IsNullOrEmpty(Creator))
            {
                sql += "and Creator like @Creator ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { TypeCode = $"%{TypeCode}%", TypeName = $"%{TypeName}%", Creator = $"%{Creator}%" });
            return count;
        }
        //修改
        public async Task<bool> UpdateAsync(TeamType model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
